package com.example.voiceEvaluation;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.dto.SentenceResult;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class VoiceEvaluation {
    // 合成webapi接口地址
    private static final String WEBISE_URL = "http://api.xfyun.cn/v1/service/v1/ise";
    // 应用ID
    private static final String APPID = "5d19f9eb";
    // 接口密钥
    private static final String API_KEY = "fa73b0a74ec2de3632c7ca1e09ab54b7";
    // 评测文本
    private static String TEXT = "";
    // 音频编码
    private static final String AUE = "raw";
    // 采样率
    private static final String AUF = "audio/L16;rate=16000";
    // 结果级别
    private static final String RESULT_LEVEL = "entirety";
    // 语种
    private static final String LANGUAGE = "en_us";
    // 评测种类
    private static final String CATEGORY = "read_sentence";
    // 音频文件地址
    private static String AUDIO_PATH = "";
    //sentence对象
    private static SentenceResult sentence =new SentenceResult();

    public static List<SentenceResult> voiceEvaluation(String text, String path) throws IOException {
        AUDIO_PATH = path;
        TEXT = text;
        Map<String, String> header = buildHttpHeader();
        byte[] audioByteArray = FileUtil.read(AUDIO_PATH);
        String audioBase64 = new String(Base64.encodeBase64(audioByteArray), "UTF-8");
        String result = HttpUtil.doPost1(WEBISE_URL, header, "audio=" + URLEncoder.encode(audioBase64, "UTF-8") + "&text=" + URLEncoder.encode(TEXT, "UTF-8"));
        return DisplayGrade(result);
    }
    public static  List<SentenceResult> DisplayGrade(String result){
        List<SentenceResult> sentenceResults = new ArrayList<SentenceResult>();
        SentenceResult sentenceResult = new SentenceResult();
        String Str,score;
        int count,i=0,j=0;
        JSONObject json,json3;
        System.out.println(result);
        json = JSON.parseObject(result);
        Str = json.getString("data");
        json =JSON.parseObject(Str);
        Str =json.getString("read_sentence");
        json = JSON.parseObject(Str);
        Str = json.getString("rec_paper");
        json = JSON.parseObject(Str);
        Str = json.getString("read_chapter");
        json = JSON.parseObject(Str);
        Str = json.getString("sentence");
        json = JSON.parseObject(Str);
        sentenceResult.setSentence(json.getString("content"));
        sentenceResult.setScore(Double.parseDouble(json.getString("total_score")));
        count = json.getInteger("word_count");
        sentenceResults.add(0,sentenceResult);
        JSONArray json2 = json.getJSONArray("word");
        while(i<count)
        {
            SentenceResult sentenceResult1 = new SentenceResult();
            json3 = json2.getJSONObject(j++);
            score =json3.getString("total_score");
            String str=json3.getString("content");
            while(str.equals("sil")||str.equals("fil")) {
                json3 = json2.getJSONObject(j++);
                score =json3.getString("total_score");
                str=json3.getString("content");
            }
            sentenceResult1.setSentence(str);
            sentenceResult1.setScore(Double.parseDouble(score));
            sentenceResults.add(sentenceResult1);
            i++;
        }
        return sentenceResults;
    }



    private static Map<String, String> buildHttpHeader() throws UnsupportedEncodingException {
        String curTime = System.currentTimeMillis() / 1000L + "";
        String param = "{\"auf\":\"" + AUF + "\",\"aue\":\"" + AUE + "\",\"result_level\":\"" + RESULT_LEVEL + "\",\"language\":\"" + LANGUAGE + "\",\"category\":\"" + CATEGORY + "\"}";
        String paramBase64 = new String(Base64.encodeBase64(param.getBytes("UTF-8")));
        String checkSum = DigestUtils.md5Hex(API_KEY + curTime + paramBase64);
        Map<String, String> header = new HashMap<String, String>();
        header.put("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
        header.put("X-Param", paramBase64);
        header.put("X-CurTime", curTime);
        header.put("X-CheckSum", checkSum);
        header.put("X-Appid", APPID);
        return header;
    }

}
